home *** CD-ROM | disk | FTP | other *** search
- Path: news.microsoft.com!news
- From: a-cnadc@microsoft.com (Dann Corbit)
- Newsgroups: comp.lang.c
- Subject: Re: Finding a prime number
- Date: 19 Feb 1996 18:36:37 GMT
- Organization: Microsoft Corporation
- Message-ID: <4gafvl$23k@news.microsoft.com>
- References: <DMpuHt.3yL@cwi.nl> <4fl2tl$ln6@ns2.emirates.net.ae> <4fnnfuINNib7@keats.ugrad.cs.ubc.ca> <4fp2kt$pks@oban.cc.ic.ac.uk <1996Feb14.070305.19468@lafn.org>
- NNTP-Posting-Host: 157.57.171.202
- Mime-Version: 1.0
- X-Newsreader: WinVN 0.93.14
-
- In article <1996Feb14.070305.19468@lafn.org>, an234@lafn.org says...
- >
- >
- >In a previous article, dik@cwi.nl (Dik T. Winter) says:
- >
- >>In article <4fp2kt$pks@oban.cc.ic.ac.uk> a.kruczkowski@ic.ac.uk (Alex Kruczkowski) writes:
- >> > Any comments or am I way off here? ;-)
- >>
- >>Way off I think. There is no repeating pattern in the list of prime
- >>numbers.
- >>--
- >>dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924098
- >>home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
- >>
- >Ok... Here is some code
- >
- >#include <stdio.h>
- >#include <conio.h>
- >
- >int is_prime(int n)
- >//return 0 if not prime
- >//return 1 if prime
- >{
- > int i,temp=0;
- > for(i=2; i==n-1, n%i!=0; i++)
- > {
- > }
- > /* If i==n or n ==1 then n is prime */
- > if (i==n || n==1) temp = 1;
- > return(temp);
- >}
- >
- >I think this works pretty well. I however don't know how fast it is..
-
- A quick speedup can be made by only testing up to sqrt(n), since if
- n has any factors, at least one of them is <= sqrt(n). For most cases
- that will make this routine run MUCH faster. A sequential list of
- primes will also reduce the work, since all numbers can be expressed
- as a unique factorization of primes. Testing with composites wastes
- effort.
- --
- The opinions expressed in this message are my own personal views
- and do not reflect the official views of Microsoft Corporation.
-
-